package com.hejian.android.a72ggametools.imageloader;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.hejian.android.a72ggametools.asynctask.DownloadImageAsyncTask;

/**
 * Created by Administrator on 2016/1/11.
 */
public class ImageLoader {
    private Context context;
    private ImageCache memoryCache;
    private static ImageLoader imageLoader = null;

    public static ImageLoader getInstance(Context context,ImageCache imageCache){
        if (imageLoader == null){
            imageLoader = new ImageLoader(context);
            imageLoader.setImageCache(imageCache);
        }
        return imageLoader;
    }

    public ImageLoader(Context context) {
        this.context = context;
    }

    public void displayImage(String urlStr, ImageView imageView){
        Log.i("zzzz", "displayImage: ............"+urlStr);
        Bitmap bitmap = memoryCache.getBitmap(urlStr);
        if (bitmap == null){
            imageView.setTag(urlStr);
            new DownloadImageAsyncTask(context,memoryCache,imageView).execute(urlStr);
        }else {
            imageView.setImageBitmap(bitmap);
        }
    }

    public void setImageCache(ImageCache imageCache){
        memoryCache = imageCache;
    }
}
